/*
 * @Author: xulanfeng 
 * @Date: 2024-10-29 11:46:36 
 * @Last Modified by: xulanfeng
 * @Last Modified time: 2024-10-29 12:02:37
 */
#include <stdio.h>
#include <time.h>
#include <string.h>

int main()
{
    time_t now;
    time(&now);
    FILE* in_h = fopen(".config","r");
    FILE* out_h = fopen("autoconf.h", "w");
    char config_buf[1024] = {0};
    char config_title[1024] = {0};
    while(NULL != fgets(config_buf,sizeof(config_buf),in_h)){
        // printf("            get .config:%s",config_buf);
        if(0 == strncmp("# Automatically generated",config_buf,strlen("# Automatically generated"))){
            memset(config_buf,0,sizeof(config_buf));
            fgets(config_title,sizeof(config_title),in_h);
            // printf("            target .config str:%s",config_title);
            break;
        }
        memset(config_buf,0,sizeof(config_buf));
    }

    fprintf(out_h, "/*\n"
		       " * Automatically generated C config: don't edit\n"
		       " * %s"
		       " * %s"
		       " */\n",
		       config_title+2, ctime(&now));

    while(NULL != fgets(config_buf,sizeof(config_buf),in_h)){
        if(0 == strncmp("CONFIG_",config_buf,strlen("CONFIG_"))){
            char* p = strchr(config_buf,'=');
            *p = '\0';
            p = p+1;
            if(*p == 'y') *p = '1';
            fprintf(out_h,"#define %s %s",config_buf,p);
        }
        memset(config_buf,0,sizeof(config_buf));
    }

    fclose(out_h);
    fclose(in_h);

    return 0;
}